package follow.juc.writing.demo2;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author tu zy
 * @version 1.0
 * @date 2020/12/5 23:27
 */

public class ThreadDemo2 {
    public static void main(String[] args) {

        TicketThread ticketThread = new TicketThread();

        Thread t1 = new Thread(ticketThread);
        t1.setName("线程1");

        Thread t2 = new Thread(ticketThread);
        t2.setName("线程2");

        Thread t3 = new Thread(ticketThread);
        t3.setName("线程3");

        t1.start();
        t2.start();
        t3.start();


    }

    static class TicketThread implements Runnable{
        private static int ticket = 100;
        Object object = new Object();
        ReentrantLock lock =  new ReentrantLock();
        @Override
        public void run() {

            while (true){
                lock.lock();
                if (ticket>0){
                    System.out.println(Thread.currentThread().getName()+"这是卖的第"+ticket+"张票");
                    ticket--;
                }else {
                    break;
                }
                lock.unlock();
            }
        }
    }
}
